package cn.itsource.pet.service.impl;

import cn.itsource.common.util.*;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.mapper.UserMapper;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.domain.SearchMasterMsgAuditLog;
import cn.itsource.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.ro.PublishRo;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public AjaxResult publish(PublishRo publishRo,Long loginInfoId) {
        //审合
//        BaiduAiUtil.TextCensor(publishRo.getName());
//        BaiduAiUtil.TextCensor(publishRo.getTitle());
        SearchMasterMsg searchMasterMsg = PublishRo.publishRoToMsg(publishRo);
        searchMasterMsg.setNote("审核通过！");
        searchMasterMsg.setState(1);
        //记录发布人的id
        searchMasterMsg.setUserId(userMapper.findUserByLoginInfoId(loginInfoId).getId());
        //分配给店铺的id  //麻烦的一匹
        //最近的店铺怎么搞   第一个点是(发布宠物的人) 第二点有是谁(商家)
        //店铺的地址
        Point point = AnalysisUtil.getPoint(publishRo.getAddress());//自己的
        List<Shop> all = shopMapper.findAll();//所有店铺的
        Shop shop = AnalysisUtil.getNearestShop(point, all);
        searchMasterMsg.setShopId(shop==null?null:shop.getId());
        super.save(searchMasterMsg);
        //审核日志的记录
        SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
        searchMasterMsgAuditLog.setAuditId(-1L);//电脑审合
        searchMasterMsgAuditLog.setMsgId(searchMasterMsg.getId());
        searchMasterMsgAuditLog.setNote("审核通过！");
        searchMasterMsgAuditLog.setAuditTime(new Date());
        searchMasterMsgAuditLog.setState(1);
        searchMasterMsgAuditLogMapper.save(searchMasterMsgAuditLog);

        return AjaxResult.me();
    }

    @Override
    public AjaxResult getUserPublishMsg(Long loginInfoId) {
        Long userId = userMapper.findUserByLoginInfoId(loginInfoId).getId();
        List<SearchMasterMsg> reuslt =  searchMasterMsgMapper.findByUserId(userId);
        return AjaxResult.me().setResultObj(reuslt);
    }

    @Override
    public PageList<SearchMasterMsg> getPool(SearchMasterMsgQuery query) {
        query.setShopNull(true);//店铺为null的查询
        //有一个固定条件   shop_id 必须为null
        PageList<SearchMasterMsg> pageList = super.findPageList(query);
        return pageList;
    }

    @Override
    public AjaxResult taking(Long msgId, Long loginInfoId) {
        Employee employee = employeeMapper.findByLoginInfoId(loginInfoId);
        if(employee.getShop_id()!=null){
            SearchMasterMsg byId = searchMasterMsgMapper.findById(msgId);
            byId.setShopId(employee.getShop_id());
            searchMasterMsgMapper.update(byId);
            return AjaxResult.me().setMessage("接单成功");
        }else {
            return AjaxResult.me().setMessage("当前账号没有绑定店铺");
        }
    }

    @Override
    public PageList<SearchMasterMsg> getHandle(SearchMasterMsgQuery query, Long loginInfoId) {
        Employee employee = employeeMapper.findByLoginInfoId(loginInfoId);
        query.setShopId(employee.getShop_id());
        //有一个固定条件   shop_id 必须为null
        PageList<SearchMasterMsg> pageList = super.findPageList(query);
        return pageList;
    }

    @Override
    public AjaxResult reject(Long msgId) {
        SearchMasterMsg searchMasterMsg = super.findById(msgId);
        searchMasterMsg.setShopId(null);
        super.update(searchMasterMsg);
        return AjaxResult.me().setMessage("拒单成功");
    }

    @Override
    public AjaxResult accept(Long msgId) {
        SearchMasterMsg searchMasterMsg = super.findById(msgId);
        searchMasterMsg.setState(2);
        super.update(searchMasterMsg);
        return AjaxResult.me().setMessage("接单成功");
    }

    @Override
    public PageList<SearchMasterMsg> getOk(SearchMasterMsgQuery query, Long loginInfoId) {
        Employee employee = employeeMapper.findByLoginInfoId(loginInfoId);
        query.setShopId(employee.getShop_id());
        query.setStateOk(false);
        //有一个固定条件   shop_id 必须为null
        PageList<SearchMasterMsg> pageList = super.findPageList(query);
        return pageList;
    }
}
